package com.clip.action;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.clip.action.util.SessionAttributeKey;
import com.clip.entity.AlertUser;
import com.clip.entity.ClipEvent;
import com.clip.entity.ClipeventPic;
import com.clip.entity.ShareEvent;
import com.clip.entity.Sharemapping;
import com.clip.entity.SystemMessage;
import com.clip.entity.User;
import com.clip.entity.virtual.EventForView;
import com.clip.service.AlertUserService;
import com.clip.service.ContactService;
import com.clip.service.EventService;
import com.clip.service.MessageService;
import com.clip.service.UserService;
import com.clip.service.UsermobileinfoService;
import com.clip.util.Struts2Utils;
import com.clip.util.ToolsUtils;
import com.clip.util.URLReplace;
import com.clip.util.json.JSONArray;
import com.clip.util.json.JSONObject;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class ManageClipAction extends ActionSupport{
	@Autowired
	private EventService eventService;
	@Autowired
	private AlertUserService alertUserService;
	@Autowired
	private UsermobileinfoService usermobileinfoService;
	@Autowired
	private ContactService contactService;
	@Autowired
	private UserService userService;
	@Autowired
	private MessageService messageService;
	private final int DETAILUSERSLENGTH=10;
	public void loadMyclips(){
		HttpServletRequest request=Struts2Utils.getRequest();
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		Calendar now=Calendar.getInstance();
		
		JSONObject returnObj=new JSONObject();
		JSONObject sub1Return=new JSONObject();
		ClipEvent clipEvent=null;
		boolean ischeckeventid=false;
		if(eventid!=null&&eventid>0){
			ischeckeventid=true;
			clipEvent=this.eventService.getDetailClipEvent(eventid);
		}else{
			if(shareid!=null&&shareid.length()>0){
				clipEvent=this.eventService.getDetailShareEvent(shareid);
			}
		}

		if(clipEvent==null){
			returnObj.put("result", 0);
			returnObj.put("errormsg", "档期不存在或无权查看");
			Struts2Utils.renderJson(returnObj.toJSONString());
			return;
		}else{
			if(clipEvent.getSeid()==0&&clipEvent.getEventtype()==2){
				
			}else if(clipEvent.getCliptype()==4){
				
			}else{
				if(ischeckeventid&&clipEvent.getUid()!=currentUser.getId()){
					returnObj.put("result", 0);
					returnObj.put("errormsg", "档期不存在或无权查看");
					Struts2Utils.renderJson(returnObj.toJSONString());
					return;
				}else if(!ischeckeventid&&clipEvent.getUid()!=currentUser.getId()){
					if(!this.eventService.checkSharedLimit(clipEvent.getId(),currentUser.getId())){
						returnObj.put("result", 0);
						returnObj.put("errormsg", "档期不存在或无权查看");
						Struts2Utils.renderJson(returnObj.toJSONString());
						return;
					}
				}
			}
		}
		int dealeventid;
		int reliedclip=clipEvent.getReliedclip();
		ClipEvent reliedClip=null;
		if(reliedclip>0){
			dealeventid=clipEvent.getReliedclip();
			clipEvent=this.eventService.getClipEventById(clipEvent.getReliedclip());
		}else{
			dealeventid=clipEvent.getId();
		}
		if(clipEvent==null){
			returnObj.put("result", 0);
			returnObj.put("errormsg", "档期不存在或无权查看");
			Struts2Utils.renderJson(returnObj.toJSONString());
			return;
		}
		sub1Return.put("eventid", dealeventid);
		sub1Return.put("title", clipEvent.getTitle());
		
		sub1Return.put("starttime",clipEvent.getBegintime());
		sub1Return.put("endtime", clipEvent.getEndtime());
		sub1Return.put("iswholeday", clipEvent.getIswholeday());
		sub1Return.put("invitetime", clipEvent.getInvitetime());
		String content=clipEvent.getContent();
		content=content==null||content.length()==0?"":content;
		sub1Return.put("content", URLReplace.replace(content));
		sub1Return.put("clocktime", clipEvent.getAlerttime());
		sub1Return.put("reliedclip",reliedclip);
		sub1Return.put("shareid", clipEvent.getShareid());
		int isself=clipEvent.getUid()==currentUser.getId()?1:0;
		sub1Return.put("isuserself", isself);
		List mylogourllist=this.userService.getUserLogoByUid(currentUser.getId());

		if(clipEvent.getReliedclip()==0){
			List fuidlogourllist=this.userService.getUserLogoByUid(clipEvent.getUid());
			String fuidcontactname=this.contactService.getUserName(currentUser.getId(),clipEvent.getUid());
			User clipUser=this.userService.getUserById(clipEvent.getUid());
			sub1Return.put("uname", fuidcontactname);
			sub1Return.put("unameid",clipEvent.getUid());
			sub1Return.put("unamelogo", fuidlogourllist.get(0));
			sub1Return.put("unameslogo", fuidlogourllist.get(1));
			if(clipUser.getUsertype()==2){
				sub1Return.put("clipiscc", 1);
			}else{
				sub1Return.put("clipiscc", 0);
			}
		}else{
			
			int ouid=reliedClip.getUid();
			User reliedClipUser=this.userService.getUserById(ouid);
			List ouidlogourllist=this.userService.getUserLogoByUid(ouid);
			String ouidcontactname=this.contactService.getUserName(currentUser.getId(),ouid);
			sub1Return.put("uname", ouidcontactname);
			sub1Return.put("unameid",ouid);
			sub1Return.put("unamelogo", ouidlogourllist.get(0));
			sub1Return.put("unameslogo", ouidlogourllist.get(1));
			if(reliedClipUser.getUsertype()==2){
				sub1Return.put("clipiscc", 1);
			}else{
				sub1Return.put("clipiscc", 0);
			}
		}
		sub1Return.put("seid", clipEvent.getSeid());
		if(clipEvent.getSeid()>0&&clipEvent.getEventtype()==2){
			ClipEvent sourceEvent=this.eventService.getDetailClipEvent(clipEvent.getSeid());
			User sourceUser=this.userService.getUserById(sourceEvent.getUid());
			sub1Return.put("sourceeventid", sourceEvent.getId());
			sub1Return.put("sourceuid", sourceEvent.getUid());
			sub1Return.put("sourceuname", this.contactService.getUserName(currentUser.getId(), sourceUser.getId()));
			List sourceuserlist=this.userService.getUserLogoByUid(sourceUser.getId());
			sub1Return.put("sourceulogo", sourceuserlist.get(1));
		}
		ClipeventPic clipeventPic=this.eventService.getClipeventPic(clipEvent.getId());
		if(clipeventPic!=null){
			sub1Return.put("clipeventpic", clipeventPic.getPicurl());
		}else{
			sub1Return.put("clipeventpic", "");
		}
		sub1Return.put("cliptype", clipEvent.getCliptype());
		sub1Return.put("clipstatus",clipEvent.getStatus());
		sub1Return.put("eventtype",clipEvent.getEventtype());
		sub1Return.put("processstatus",clipEvent.getProcessstatus());
		if(clipEvent.getCliptype()==4){
			List list=this.eventService.getStatAboutClipEvent(clipEvent,currentUser); //得到分享档期的统计
			sub1Return.put("commentcount", list.get(0));
		}else if(clipEvent.getCliptype()==3){
			if(currentUser.getUsertype()==1){
				if(clipEvent.getEventtype()==2&&(clipEvent.getSeid()==0||clipEvent.getReliedclip().equals(clipEvent.getSeid()))){
					//说明用户直接查看商家档期或者是查看直接感兴趣商家的档期
					//需要	感兴趣数		评论数	喜欢数	是否已经感兴趣	是否已经喜欢
					List list=this.eventService.getStatAboutClipEvent(clipEvent,currentUser); //得到分享档期的统计
					sub1Return.put("interestcount", list.get(0));
					sub1Return.put("forwardcount", list.get(1));
					sub1Return.put("favorcount", list.get(2));
					sub1Return.put("commentcount", list.get(3));
					List<Long> checklist=this.eventService.checkUserStatusAboutShareEvent(clipEvent,currentUser);
					sub1Return.put("eventisinterested",checklist.get(0)>0l?true:false);
					sub1Return.put("isfavor",checklist.get(1)>0l?true:false);
					int result=this.eventService.getInviteEventInterestORNOT(eventid, currentUser.getId());
					if(result>0){
						ClipEvent interestedevent=this.eventService.getDetailClipEvent(result);
						sub1Return.put("clocktime", interestedevent.getAlerttime());
					}
				}else{
					//查看个人用户的邀请或者查看感兴趣个人用户的档期
					//需要 评论数		是否感兴趣	是否一拍即合
					List list=this.eventService.getStatAboutClipEvent(clipEvent,currentUser); //得到分享档期的统计
					sub1Return.put("commentcount", list.get(0));
					int result=this.eventService.getInviteEventInterestORNOT(dealeventid, currentUser.getId());
					sub1Return.put("eventisinterested", result>0?1:0);
					if(result>0){
						ClipEvent interestedevent=this.eventService.getDetailClipEvent(result);
						sub1Return.put("clocktime", interestedevent.getAlerttime());
					}
					boolean isforward=eventService.checkClipEventForwardedORNOT(dealeventid, currentUser.getId());
					sub1Return.put("isforward", isforward);
					boolean isconfirmevent=false;
					isconfirmevent=this.eventService.checkIsInvitedEvent(dealeventid);
					sub1Return.put("isconfirmevent", isconfirmevent);
					//检查是否屏蔽
					if(isself==0||reliedclip>0){
						boolean isshielded=this.eventService.checkIsShielded(dealeventid, currentUser.getId());
						sub1Return.put("isshielded", isshielded);
					}else
						sub1Return.put("isshielded", false);
					
				}
			}else{
				//商家
				List list=this.eventService.getStatAboutClipEvent(clipEvent,currentUser); //得到分享档期的统计
				sub1Return.put("commentcount", list.get(0));
			}
			/*ClipEvent acceptShareEvent=this.eventService.getUserAcceptShareEvent(clipEvent.getId(), currentUser.getId());
			if(acceptShareEvent!=null){
				temp2.put("isshareaccept", true);
				temp2.put("acceptshareid",acceptShareEvent.getShareid()!=null&&acceptShareEvent.getShareid().length()>0?acceptShareEvent.getShareid():"");
			}else
				temp2.put("isshareaccept", false);
			List list=this.eventService.getStatAboutClipEvent(clipEvent,currentUser); //得到分享档期的统计
			temp2.put("countsharesave", list.get(0));
			temp2.put("countshareforward", list.get(1));
			
			
			int followeventid=this.eventService.getUserAcceptShareEventid(clipEvent,currentUser.getId());
			if(followeventid>0){
				List sharemapslist=this.eventService.getShareMappingList(followeventid);
				for (int i=0;i<sharemapslist.size();i++) {
					Sharemapping sharemapping=(Sharemapping) sharemapslist.get(i);
					int share2uid=sharemapping.getShare2uid();
					if(share2uid==0){
						List logolist=this.userService.getUserLogoByUid(0);
						temp2.put("sharemembers index=-1 name=全部用户",ToolsUtils.xmlCDATA(logolist.get(1)));
					}else{
						String name=this.contactService.getUserName(currentUser.getId(), share2uid);
						List logolist=this.userService.getUserLogoByUid(share2uid);
						temp2.put("sharemembers index="+i+" uid="+share2uid+" name="+name,ToolsUtils.xmlCDATA(logolist.get(1)));
					}
				}
			}*/
			
		}
		/*Map contactMap=null;
		//我发起的档期
		if(clipEvent.getReliedclip()==0&&clipEvent.getUid()==currentUser.getId()){
			AlertUser alertUser=this.eventService.getInviteEventAlertUser(clipEvent.getId(),0);
			if(alertUser!=null){
				contactMap=this.eventService.getClipInvitedPerson(id,currentUser);
				temp2.put("istoinviteclip", true);
				for(int i=0;i<contactMap.keySet().size();i++){
					Map<String,Object> temp1=new HashMap<String,Object>();
					List list=(List) contactMap.get(i+1);
					temp1.put("name", list.get(1));
					temp1.put("email", list.get(2));
					temp1.put("status", list.get(3));
					temp1.put("realname", list.get(4));
					temp2.put("contact uid="+list.get(0),temp1);
				}
			}else{
				temp2.put("istoinviteclip", false);
			}
		}else{
			AlertUser alertUser=this.eventService.getInviteEventAlertUser(clipEvent.getReliedclip(),currentUser.getId());
			if(alertUser!=null){
				contactMap=this.eventService.getClipInvitedPerson(id,currentUser);
				temp2.put("isinvitedclip", true);
				temp2.put("alertstatus", alertUser.getStatus());
				for(int i=0;i<contactMap.keySet().size();i++){
					Map<String,Object> temp1=new HashMap<String,Object>();
					List list=(List) contactMap.get(i+1);
					temp1.put("name", list.get(1));
					temp1.put("email", list.get(2));
					temp1.put("status", list.get(3));
					temp1.put("realname", list.get(4));
					temp2.put("contact uid="+list.get(0),temp1);
				}
			}else{
				temp2.put("isinvitedclip", false);
			}
		}
		
		returnMap.put("result", true);
		returnMap.put("ClipEvent id="+clipEvent.getId(), temp2);
		boolean flag=usermobileinfoService.isHasBindableMobile(currentUser.getId());
		int temp=flag?1:0;
		returnMap.put("mobilenotice", temp);*/
		returnObj.put("result", 1);
		returnObj.put("clipevent", sub1Return);
		boolean flag=usermobileinfoService.isHasBindableMobile(currentUser.getId());
		int temp=flag?1:0;
		returnObj.put("mobilenotice", temp);
		Struts2Utils.renderJson(returnObj.toJSONString());
	}
	
	public void loadEventDetail(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		ClipEvent clipEvent=null;
		if(eventid==0){
			clipEvent=(ClipEvent) session.getAttribute(SessionAttributeKey.TEMPCLIP);
			session.removeAttribute(SessionAttributeKey.TEMPCLIP);
		}else
			clipEvent=this.eventService.getClipEventById(eventid);
		if(clipEvent==null){
			Struts2Utils.renderText("");
		}else{
			if(clipEvent.getUid()!=currentUser.getId()){
				Struts2Utils.renderText("");
				return;
			}
			Map reMap=new HashMap();
			reMap.put("eventid", clipEvent.getId());
			reMap.put("title", ToolsUtils.xmlCDATA(clipEvent.getTitle()));
			reMap.put("address", clipEvent.getAddress()==null||clipEvent.getAddress().length()==0?"":ToolsUtils.xmlCDATA(clipEvent.getAddress()));
			reMap.put("content", clipEvent.getContent()==null||clipEvent.getContent().length()==0?"":ToolsUtils.xmlCDATA(clipEvent.getContent()));
			reMap.put("eventtype", ClipEvent.eventtypeInt2Str(clipEvent.getEventtype()));
			reMap.put("starttime", clipEvent.getBegintime());
			reMap.put("endtime", clipEvent.getEndtime());
			reMap.put("iswholeday",clipEvent.getIswholeday());
			Struts2Utils.renderXml(ToolsUtils.getDOM(reMap));
		}
	}
	
	public void loadCacheAndClockAndRecycleCount(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Long alerttotalcount=this.eventService.queryClipListTotalCount("alert",currentUser,null,null);
		Long memototalcount=this.eventService.queryClipListTotalCount("memo",currentUser,null,null);
		Long invitetotalcount=this.eventService.queryClipListTotalCount("invite",currentUser,null,null);
		Long cachetotalcount=this.eventService.queryClipListTotalCount("cache",currentUser,null,null);
		Long recycletotalcount=this.eventService.queryClipListTotalCount("recycle",currentUser,null,null);
		StringBuffer str=new StringBuffer();
		str.append("{result:1,alerttotalcount:");
		str.append(alerttotalcount);
		str.append(",memototalcount:");
		str.append(memototalcount);
		str.append(",invitetotalcount:");
		str.append(invitetotalcount);
		str.append(",cachetotalcount:");
		str.append(cachetotalcount);
		str.append(",recycletotalcount:");
		str.append(recycletotalcount);
		str.append("}");
		Struts2Utils.renderText(str.toString());
	}
	
	public void getEventsByTodayAndTommorrow(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Calendar now=Calendar.getInstance();
		Calendar start=(Calendar) now.clone();
		start.clear(Calendar.MILLISECOND);
		start.clear(Calendar.SECOND);
		start.clear(Calendar.MINUTE);
		start.set(Calendar.HOUR_OF_DAY, 0);
		Calendar end=(Calendar) start.clone();
		end.add(Calendar.DATE,1);
		List events1=eventService.getEventsByPassage(start,end,now,currentUser);
		Calendar end2=(Calendar) end.clone();
		end2.add(Calendar.DATE,1);
		List events2=eventService.getEventsByPassage(end,end2,now,currentUser);
		Map<String,Map> returnmap=organizeTodayAndTommorrowViewMapAsXml(events1,events2);
		Struts2Utils.renderXml(ToolsUtils.getDOM(returnmap));
	}
	
	public void getEventsByInviting(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Calendar now=Calendar.getInstance();
		now.clear(Calendar.MILLISECOND);
		now.clear(Calendar.SECOND);
		List events1=eventService.getEventsByLast24(currentUser,now);
		List events2=eventService.getEventsByUrgent24h(currentUser,now);
		Map returnmap=organizeLastAndUrgentViewMapAsXml(events1,events2);
		boolean flag=usermobileinfoService.isHasBindableMobile(currentUser.getId());
		int temp=flag?1:0;
		returnmap.put("mobilenotice", temp);
		Struts2Utils.renderXml(ToolsUtils.getDOM(returnmap));
	}
	
	public void ignoreInvitedClip(){
		this.eventService.ignoreInvitedClip(eventid);
		Struts2Utils.renderText("{result:1}");
	}
	
	public void refuseInvitedClip(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		this.eventService.refuseInvitedClip(eventid,currentUser);
		Struts2Utils.renderText("{result:1}");
	}
	public void loadTodayEvents(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Calendar now=Calendar.getInstance();
		now.clear(Calendar.MILLISECOND);
		now.clear(Calendar.SECOND);
		now.clear(Calendar.MINUTE);
		now.set(Calendar.HOUR_OF_DAY, 0);
//		List events=eventService.getEventsByDay(now,now,currentUser);
		Map reMap=new HashMap();
		reMap.put("day",now.getTimeInMillis());
		boolean flag=usermobileinfoService.isHasBindableMobile(currentUser.getId());
		int temp=flag?1:0;
		reMap.put("mobilenotice", temp);
		Struts2Utils.renderXml(ToolsUtils.getDOM(reMap));
	}
	
	public void loadDefinedDateEvents(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Calendar now=Calendar.getInstance();
		Calendar calendar=Calendar.getInstance();
		calendar.setTimeInMillis(this.definedLong);
		calendar.clear(Calendar.MILLISECOND);
		calendar.clear(Calendar.SECOND);
		calendar.clear(Calendar.MINUTE);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		List events=eventService.getEventsByDay(calendar,now,currentUser);
		Map reMap=organizeViewMapAsXml(events);
		reMap.put("day",calendar.getTimeInMillis());
		Struts2Utils.renderXml(ToolsUtils.getDOM(reMap));
	}
	
	private int CLIPLISTPAGESIZE=15;
	public void loadMainpartRecords(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Calendar dateTime=Calendar.getInstance();
		Calendar now=(Calendar) dateTime.clone();
		if(this.year==0&&this.month==0&&this.date==0){
			dateTime=null;
		}else{
			dateTime.clear(Calendar.MILLISECOND);
			dateTime.clear(Calendar.SECOND);
			dateTime.clear(Calendar.MINUTE);
			dateTime.set(Calendar.HOUR_OF_DAY, 0);
			dateTime.set(Calendar.YEAR,this.year);
			dateTime.set(Calendar.MONTH,this.month);
			dateTime.set(Calendar.DAY_OF_MONTH, this.date);
		}
		Long totalcount=this.eventService.queryClipListTotalCount(queryby,currentUser,content,dateTime);
		Integer pageCount=this.eventService.queryClipListPageCount(totalcount);
		if(currentpage+1>pageCount){
			currentpage=pageCount-1;
		}else if(currentpage<0){
			currentpage=0;
		}
		List list=this.eventService.queryClipList(queryby,currentUser,currentpage,CLIPLISTPAGESIZE,0l,content,dateTime,orderby,isDesc==1?true:false);
		Map map=new LinkedHashMap();
		if(queryby.equals("cache")){
			for (Object object : list) {
				ClipEvent clipevent=(ClipEvent) object;
				Map submap=new HashMap();
				submap.put("title", ToolsUtils.xmlCDATA(clipevent.getTitle()));
				submap.put("starttime", clipevent.getBegintime().toString());
				submap.put("iswholeday", clipevent.getIswholeday().toString());
				submap.put("endtime", clipevent.getEndtime().toString());
				submap.put("clocktime", clipevent.getAlerttime().toString());
				submap.put("createtime", clipevent.getCreatetime().toString());
				submap.put("updatetime", clipevent.getUpdatetime().toString());
				map.put("ClipEvent id="+clipevent.getId(),submap);
			}
		}else{
			for (Object object : list) {
				Map submap=null;
				int isself = 0,reliedclip = 0,eventid = 0;
				if(object instanceof ShareEvent){
					ShareEvent sharevent=(ShareEvent) object;
					submap=orgShareEvents(sharevent,currentUser,now);
					isself=sharevent.getUid()==currentUser.getId()?1:0;
					reliedclip=sharevent.getReliedclip();
					eventid=sharevent.getEventid();
				}else{
					ClipEvent clipevent=(ClipEvent) object;
					if(clipevent.getCliptype()==2){
						submap=orgMemoEvent(clipevent,currentUser);
					}else
						submap=orgAllClipEvents(clipevent,currentUser,now);
					isself=clipevent.getUid()==currentUser.getId()?1:0;
					reliedclip=clipevent.getReliedclip();
					eventid=clipevent.getId();
				}
				if(isself==1){
					//检查该分享档期是否被保存过
					if(reliedclip==0){
						AlertUser alertUser=this.eventService.getInviteEventAlertUser(eventid,0);
						if(alertUser!=null){
							submap.put("isinviteevent", true);
						}else{
							submap.put("isinviteevent", false);
						}
					}else{
						AlertUser alertUser=this.eventService.getInviteEventAlertUser(reliedclip,currentUser.getId());
						if(alertUser!=null){
							submap.put("isinviteevent", true);
							submap.put("alertstatus", alertUser.getStatus());
						}else{
							submap.put("isinviteevent", false);
						}
					}
				}else{
					submap.put("isinviteevent", false);
				}
				map.put("ClipEvent isuserself="+isself+" id="+eventid,submap);
			}
		}
		int crtRecordsCount=0;
		if((currentpage+1)==pageCount){
			crtRecordsCount=totalcount.intValue()-EventService.PAGESIZE*currentpage;
		}else{
			crtRecordsCount=EventService.PAGESIZE;
		}
		Long alerttotalcount=this.eventService.queryClipListTotalCount("alert",currentUser,content,dateTime);
		Long memototalcount=this.eventService.queryClipListTotalCount("memo",currentUser,content,dateTime);
		Long invitetotalcount=this.eventService.queryClipListTotalCount("invite",currentUser,content,dateTime);
		Long cachetotalcount=this.eventService.queryClipListTotalCount("cache",currentUser,null,null);
		Long recycletotalcount=this.eventService.queryClipListTotalCount("recycle",currentUser,null,null);
		map.put("alerttotalcount", alerttotalcount);
		map.put("memototalcount", memototalcount);
		map.put("invitetotalcount", invitetotalcount);
		map.put("cachetotalcount", cachetotalcount);
		map.put("recycletotalcount", recycletotalcount);
		map.put("totalcount", totalcount);
		map.put("pagecount", pageCount);
		map.put("currentrecordcount", crtRecordsCount);
		map.put("currentpage", currentpage);
		Struts2Utils.renderXml(ToolsUtils.getDOM(map));
	}
	
	private Map orgMemoEvent(ClipEvent memoevent,User currentUser){
		Map<String,Object> temp2=new HashMap<String,Object>();
		temp2.put("starttime", memoevent.getBegintime().toString());
		temp2.put("clocktime", memoevent.getAlerttime().toString());
		temp2.put("createtime", memoevent.getCreatetime().toString());
		temp2.put("title", ToolsUtils.xmlCDATA(memoevent.getTitle()));
		temp2.put("cliptype", memoevent.getCliptype());
		temp2.put("iswholeday", 1);
		return temp2;
	}
	
	public void searchMainpartRecords(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Long totalcount=this.eventService.queryClipListTotalCount("search",currentUser,content,null);
		Integer pageCount=this.eventService.queryClipListPageCount(totalcount);
		if(currentpage+1>pageCount){
			currentpage=pageCount-1;
		}else if(currentpage<0){
			currentpage=0;
		}
		List<ClipEvent> list=this.eventService.queryClipList("search",currentUser,currentpage,CLIPLISTPAGESIZE,0l,content,null,null,true);
		JSONObject jsonObject=new JSONObject();
		JSONArray array=new JSONArray();
		for (ClipEvent clipEvent : list) {
			JSONObject sub1Return=new JSONObject();
			sub1Return.put("eventid", clipEvent.getId());
			sub1Return.put("title", clipEvent.getTitle());
			sub1Return.put("isuserself", clipEvent.getUid()==currentUser.getId());
			sub1Return.put("starttime",clipEvent.getBegintime());
			sub1Return.put("endtime", clipEvent.getEndtime());
			sub1Return.put("iswholeday", clipEvent.getIswholeday());
			String content=clipEvent.getContent();
			content=content==null||content.length()==0?"":content;
			sub1Return.put("content", URLReplace.replace(content));
			sub1Return.put("clocktime", clipEvent.getAlerttime());
			sub1Return.put("reliedclip",0);
			sub1Return.put("shareid", clipEvent.getShareid());
			sub1Return.put("createtime", clipEvent.getCreatetime());
			if(clipEvent.getReliedclip()==0){
				List fuidlogourllist=this.userService.getUserLogoByUid(clipEvent.getUid());
				String fuidcontactname=this.contactService.getUserName(currentUser.getId(),clipEvent.getUid());
				User clipUser=this.userService.getUserById(clipEvent.getUid());
				sub1Return.put("uname", fuidcontactname);
				sub1Return.put("unameid",clipEvent.getUid());
				sub1Return.put("unamelogo", fuidlogourllist.get(0));
				sub1Return.put("unameslogo", fuidlogourllist.get(1));
				if(clipUser.getUsertype()==2){
					sub1Return.put("clipiscc", 1);
				}else{
					sub1Return.put("clipiscc", 0);
				}
			}
			sub1Return.put("seid", clipEvent.getSeid());
			sub1Return.put("cliptype", clipEvent.getCliptype());
			sub1Return.put("clipstatus",clipEvent.getStatus());
			sub1Return.put("eventtype",clipEvent.getEventtype());
			sub1Return.put("processstatus",clipEvent.getProcessstatus());
			array.add(sub1Return);
		}
		jsonObject.put("searchresults",array);
		int crtRecordsCount=0;
		if((currentpage+1)==pageCount){
			crtRecordsCount=totalcount.intValue()-EventService.PAGESIZE*currentpage;
		}else{
			crtRecordsCount=EventService.PAGESIZE;
		}
		jsonObject.put("totalcount", totalcount);
		jsonObject.put("pagecount", pageCount);
		jsonObject.put("currentrecordcount", crtRecordsCount);
		jsonObject.put("currentpage", currentpage);
		Struts2Utils.renderJson(jsonObject.toJSONString());
	}
	
	public Map orgAllClipEvents(ClipEvent clipevent,User currentUser,Calendar now){
		Map<String,Object> temp2=new LinkedHashMap<String,Object>();
		boolean isself=clipevent.getUid()==currentUser.getId();
		int reliedclip=clipevent.getReliedclip();
		int uid=clipevent.getUid();
		int seid=clipevent.getSeid();
		int eventtype=clipevent.getEventtype();
		int cliptype=clipevent.getCliptype();
		boolean clipiscc=false;
		if(reliedclip>0){
			ClipEvent parentEvent=this.eventService.getClipEventById(reliedclip);
			if(parentEvent!=null){
				int parentuid=parentEvent.getUid();
				User parentUser=this.userService.getUserById(parentuid);
				if(eventtype==2&&seid==reliedclip){
					clipiscc=true;
					temp2.put("clipiscc", 1);
				}else{
					temp2.put("clipiscc", 0);
				}
				if(parentEvent.getUid()==currentUser.getId()){
					//不可能发生
					temp2.put("isself", true);
					temp2.put("unameid",parentuid);
					temp2.put("uname", this.contactService.getUserName(currentUser.getId(),parentuid));
					List urllist=this.userService.getUserLogoByUid(parentuid);
					temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
				}else{
					temp2.put("isself", false);
					String contactname=this.contactService.getUserName(currentUser.getId(),parentuid);
					temp2.put("unameid",parentuid);
					temp2.put("uname", contactname);
					List urllist=this.userService.getUserLogoByUid(parentuid);
					temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
				}
			}else{
				temp2.put("uname", "我");
				temp2.put("unameid", currentUser.getId());
				                            
				List urllist=this.userService.getUserLogoByUid(currentUser.getId());
				temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
			}
			temp2.put("eventisinterested", 1);
		}else{
			if(uid==currentUser.getId()){
				temp2.put("isself", true);
				temp2.put("uname", "我");
				temp2.put("unameid", currentUser.getId());
				List urllist=this.userService.getUserLogoByUid(currentUser.getId());
				temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
				temp2.put("pname", "");
				temp2.put("eventisinterested", 0);
			}else{
				temp2.put("isself", false);
				String contactname=this.contactService.getUserName(currentUser.getId(),uid);
				temp2.put("unameid",uid);
				temp2.put("uname", contactname);
				List urllist=this.userService.getUserLogoByUid(uid);
				temp2.put("unamelogo", ToolsUtils.xmlCDATA(urllist.get(1)));
				temp2.put("pname", "");
				int result=this.eventService.getInviteEventInterestORNOT(clipevent.getId(), currentUser.getId());
				temp2.put("eventisinterested", result>0?1:0);
			}
		}
		temp2.put("cliptype", clipevent.getCliptype().toString());
		temp2.put("shareid", clipevent.getShareid());
		temp2.put("starttime", clipevent.getBegintime().toString());
		temp2.put("endtime", clipevent.getEndtime().toString());
		temp2.put("iswholeday", clipevent.getIswholeday().toString());
		temp2.put("invitetime", clipevent.getInvitetime().toString());
//		temp2.put("deadlinetime", clipevent.getDeadlinetime().toString());
//		temp2.put("isexpire", clipevent.getDeadlinetime()<=now.getTimeInMillis());
		temp2.put("content", ToolsUtils.xmlCDATA(clipevent.getContent()));
		temp2.put("address", ToolsUtils.xmlCDATA(clipevent.getAddress()));
		temp2.put("clipstatus", clipevent.getStatus());
		temp2.put("title", ToolsUtils.xmlCDATA(clipevent.getTitle()));
		temp2.put("eventtype", ClipEvent.eventtypeInt2Str(clipevent.getEventtype()));
		int timesituation=checkShareEventTimeSituation(clipevent);
		temp2.put("timesituation", timesituation);
		temp2.put("reliedclip", clipevent.getReliedclip().toString());
		eventService.updateOverDeadlinetime(clipevent,now);
		temp2.put("processstatus", clipevent.getProcessstatus().toString());
		temp2.put("clocktime", clipevent.getAlerttime().toString());
		temp2.put("createtime", clipevent.getCreatetime());
		temp2.put("seid", clipevent.getSeid());
		temp2.put("eventtype", clipevent.getEventtype());
		if(cliptype==3){
			if(clipiscc==false){
//				boolean isforward=this.eventService.checkClipEventForwardedORNOT(reliedclip>0?reliedclip:clipevent.getId(), currentUser.getId());
//				temp2.put("isforward", isforward);
				boolean isconfirmevent=false;
				if(reliedclip>0)
					isconfirmevent=this.eventService.checkIsInvitedEvent(reliedclip);
				else
					isconfirmevent=this.eventService.checkIsInvitedEvent(clipevent.getId());
				temp2.put("isconfirmevent", isconfirmevent);
			}
		}
		return temp2;
	}
	
	private Map orgShareEvents(ShareEvent sharevent, User currentUser,
			Calendar now) {
		ClipEvent clipevent=this.eventService.getClipEventById(sharevent.getEventid());
		return orgAllClipEvents(clipevent,currentUser,now);
	}
	
	private int checkShareEventTimeSituation(ClipEvent clipEvent){
		Calendar firstDate=Calendar.getInstance();
		firstDate.clear(Calendar.MILLISECOND);
		firstDate.clear(Calendar.SECOND);
		firstDate.clear(Calendar.MINUTE);
		firstDate.set(Calendar.HOUR_OF_DAY, 0);
		firstDate.set(Calendar.YEAR, year);
		firstDate.set(Calendar.MONTH, month);
		firstDate.set(Calendar.DAY_OF_MONTH,date);
		Calendar secondDate=(Calendar) firstDate.clone();
		secondDate.add(Calendar.DATE, 1);
		if(clipEvent.getBegintime()>=firstDate.getTimeInMillis()&&clipEvent.getBegintime()<secondDate.getTimeInMillis()){
			return -1;
		}else if(clipEvent.getEndtime()>=firstDate.getTimeInMillis()&&clipEvent.getEndtime()<secondDate.getTimeInMillis()&&clipEvent.getIswholeday()==0){
			return 1;
		}else{
			return 0;
		}
	}
	
	public void loadCacheMainpartRecords(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Long totalcount=this.eventService.queryCacheClipListTotalCount(currentUser);
		Integer pageCount=this.eventService.queryClipListPageCount(totalcount);
		if(currentpage+1>pageCount){
			currentpage=pageCount-1;
		}else if(currentpage<0){
			currentpage=0;
		}
		List<EventForView> list=this.eventService.queryCacheClipList(currentUser,currentpage);
		Map reMap=organizeCacheViewMapAsXml(list);
		int crtRecordsCount=0;
		if((currentpage+1)==pageCount){
			crtRecordsCount=totalcount.intValue()-EventService.PAGESIZE*currentpage;
		}else{
			crtRecordsCount=EventService.PAGESIZE;
		}
		Long clocktotalcount=this.eventService.queryRemindClipListTotalCount(currentUser,0,0,0);
		Long recycletotalcount=this.eventService.queryRecycleClipListTotalCount(currentUser);
		reMap.put("totalcount", totalcount);
		reMap.put("clocktotalcount", clocktotalcount);
		reMap.put("recycletotalcount", recycletotalcount);
		reMap.put("pagecount", pageCount);
		reMap.put("currentrecordcount", crtRecordsCount);
		reMap.put("currentpage", currentpage);
		Struts2Utils.renderXml(ToolsUtils.getDOM(reMap));
	}
	
	public void loadRemindMainpartRecords(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Calendar now=Calendar.getInstance();
		Long totalcount=this.eventService.queryRemindClipListTotalCount(currentUser,year,month,date);
		Integer pageCount=this.eventService.queryClipListPageCount(totalcount);
		if(currentpage+1>pageCount){
			currentpage=pageCount-1;
		}else if(currentpage<0){
			currentpage=0;
		}
		List<EventForView> list=this.eventService.queryRemindClipList(currentUser,currentpage,year,month,date);
		Map reMap=organizeRemindViewMapAsXml(list);
		int crtRecordsCount=0;
		if((currentpage+1)==pageCount){
			crtRecordsCount=totalcount.intValue()-EventService.PAGESIZE*currentpage;
		}else{
			crtRecordsCount=EventService.PAGESIZE;
		}
		Long ctotalcount=this.eventService.queryCacheClipListTotalCount(currentUser);
		Long recycletotalcount=this.eventService.queryRecycleClipListTotalCount(currentUser);
		Long clocktotalcount=this.eventService.queryRemindClipListTotalCount(currentUser,0,0,0);
		reMap.put("totalcount", totalcount);
		reMap.put("remindtotalcount", clocktotalcount);
		reMap.put("cachetotalcount", ctotalcount);
		reMap.put("recycletotalcount", recycletotalcount);
		reMap.put("pagecount", pageCount);
		reMap.put("currentrecordcount", crtRecordsCount);
		reMap.put("currentpage", currentpage);
		Struts2Utils.renderXml(ToolsUtils.getDOM(reMap));
	}
	
	public void loadRecycleMainpartRecords(){
		HttpSession session=Struts2Utils.getSession();
		User currentUser=(User) session.getAttribute(SessionAttributeKey.USER);
		Long totalcount=this.eventService.queryRecycleClipListTotalCount(currentUser);
		Integer pageCount=this.eventService.queryClipListPageCount(totalcount);
		if(currentpage+1>pageCount){
			currentpage=pageCount-1;
		}else if(currentpage<0){
			currentpage=0;
		}
		List<EventForView> list=this.eventService.queryRecycleClipList(currentUser,currentpage);
		Map reMap=organizeViewMapAsXml(list);
		int crtRecordsCount=0;
		if((currentpage+1)==pageCount){
			crtRecordsCount=totalcount.intValue()-EventService.PAGESIZE*currentpage;
		}else{
			crtRecordsCount=EventService.PAGESIZE;
		}
		Long ctotalcount=this.eventService.queryCacheClipListTotalCount(currentUser);
		Long clocktotalcount=this.eventService.queryRemindClipListTotalCount(currentUser,0,0,0);
		reMap.put("clocktotalcount", clocktotalcount);
		reMap.put("cachetotalcount", ctotalcount);
		reMap.put("totalcount", totalcount);
		reMap.put("pagecount", pageCount);
		reMap.put("currentrecordcount", crtRecordsCount);
		reMap.put("currentpage", currentpage);
		Struts2Utils.renderXml(ToolsUtils.getDOM(reMap));
	}
	
//	public void acceptInvitedClip(){
//		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
//		ClipEvent clipEvent = this.eventService.acceptInvitedClip(currentUser,eventid);
//		if(clipEvent!=null){
//			Struts2Utils.renderText("{result:'接受邀请成功',flag:1}");
//		}
//	}
	
	public void loadDetailMemoevent(){
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		Calendar now=Calendar.getInstance();
		if(year==0||date==0){
			year=now.get(Calendar.YEAR);
			month=now.get(Calendar.MONTH);
			date=now.get(Calendar.DAY_OF_MONTH);
		}
		Calendar temp=Calendar.getInstance();
		temp.set(Calendar.YEAR,year);
		temp.set(Calendar.MONTH, month);
		temp.set(Calendar.DAY_OF_MONTH, date);
		temp.clear(Calendar.MILLISECOND);
		temp.clear(Calendar.SECOND);
		temp.clear(Calendar.MINUTE);
		temp.set(Calendar.HOUR_OF_DAY,0);
		List list=this.eventService.getMemoEventsByDay(temp, now, currentUser);
		Map<String, Map> returnMap=new LinkedHashMap<String, Map>();
		for (Object object : list) {
			EventForView eventforview=(EventForView) object;
			Map<String,Object> temp2=new HashMap<String,Object>();
			temp2.put("eventid", eventforview.getEventid());
			temp2.put("starttime", eventforview.getStarttime().toString());
			temp2.put("clocktime", eventforview.getClocktime()==null?"":eventforview.getClocktime());
			temp2.put("title", eventforview.getTitle());
			//temp2.put("date", firstDateOfWeek.getTimeInMillis());
			returnMap.put("EventForView eventid="+eventforview.getEventid(), temp2);
		}
		Struts2Utils.renderXml(ToolsUtils.getDOM(returnMap));
	}
	
	public void loadAllMemoevent(){
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		Calendar now=Calendar.getInstance();
		List list=this.eventService.getMemoEvents(now,currentUser,currentpage);
		Integer count=this.eventService.countMemoEventsByDay(null, currentUser);
		int perpagelength=EventService.MEMOPERPAGE;
		int pagecount=new Double(Math.ceil(count.doubleValue()/new Double(perpagelength))).intValue();
		Map<String, Object> returnMap=new LinkedHashMap<String, Object>();
		for (Object object : list) {
			ClipEvent clipEvent=( ClipEvent) object;
			Map<String,Object> temp2=new HashMap<String,Object>();
			temp2.put("eventid", clipEvent.getId());
			temp2.put("starttime", clipEvent.getBegintime().toString());
			temp2.put("clocktime", clipEvent.getAlerttime()==null?"":clipEvent.getAlerttime());
			temp2.put("title", clipEvent.getTitle());
			returnMap.put("EventForView eventid="+clipEvent.getId(), temp2);
		}
		returnMap.put("pagecount", pagecount);
		returnMap.put("currentpage", currentpage);
		Struts2Utils.renderXml(ToolsUtils.getDOM(returnMap));
	}
	/**
	 * @param events1
	 * @param events2
	 * @return
	 * 
	 * timeattr 1:最近24个邀请档期
	 * 			2:最急的24小时内的档期
	 */
	private Map<String, Map> organizeLastAndUrgentViewMapAsXml(List events1,List events2) {
		Map<String, Map> returnMap=new LinkedHashMap<String, Map>();
		Calendar now=Calendar.getInstance();
		for (Object object : events1) {
			EventForView eventforview=(EventForView) object;
			Map<String,Object> temp2=new HashMap<String,Object>();
			temp2.put("eventid", eventforview.getEventid());
			temp2.put("isClocked", ""+eventforview.isClocked());
			temp2.put("eventtype", eventforview.getEventtype());
			temp2.put("starttime", eventforview.getStarttime().toString());
			temp2.put("endtime", eventforview.getEndtime().toString());
			temp2.put("iswholeday", eventforview.getIswholeday().toString());
			temp2.put("content", eventforview.getContent());
			temp2.put("clocktime", eventforview.getClocktime()==null?"":eventforview.getClocktime());
			temp2.put("address", eventforview.getAddress());
			temp2.put("title", eventforview.getTitle());
			temp2.put("deadlinetime", eventforview.getDeadlinetime().toString());
			temp2.put("isexpire", eventforview.getDeadlinetime()<=now.getTimeInMillis());
			temp2.put("reliedclip", eventforview.getReliedclip().toString());
			temp2.put("isvip", eventforview.getIsvip().toString());
			temp2.put("puid", eventforview.getPuid());
			temp2.put("publishname", eventforview.getPublishname());
			temp2.put("processstatus", eventforview.getProcessstatus().toString());
			//temp2.put("date", firstDateOfWeek.getTimeInMillis());
			returnMap.put("EventForView timeattr="+1+" id="+eventforview.getEventid(), temp2);
		}
		for (Object object : events2) {
			EventForView eventforview=(EventForView) object;
			Map<String,Object> temp2=new HashMap<String,Object>();
			temp2.put("eventid", eventforview.getEventid());
			temp2.put("isClocked", ""+eventforview.isClocked());
			temp2.put("eventtype", eventforview.getEventtype());
			temp2.put("starttime", eventforview.getStarttime().toString());
			temp2.put("endtime", eventforview.getEndtime().toString());
			temp2.put("content",eventforview.getContent());
			temp2.put("clocktime", eventforview.getClocktime()==null?"":eventforview.getClocktime());
			temp2.put("address", eventforview.getAddress());
			temp2.put("title", eventforview.getTitle());
			temp2.put("deadlinetime", eventforview.getDeadlinetime().toString());
			temp2.put("isexpire", eventforview.getDeadlinetime()<=now.getTimeInMillis());
			temp2.put("reliedclip", eventforview.getReliedclip().toString());
			temp2.put("isvip", eventforview.getIsvip().toString());
			temp2.put("publishname", eventforview.getPublishname());
			temp2.put("puid", eventforview.getPuid());
			temp2.put("processstatus", eventforview.getProcessstatus().toString());
			//temp2.put("date", firstDateOfWeek.getTimeInMillis());
			returnMap.put("EventForView timeattr="+2+" id="+eventforview.getEventid(), temp2);
		}
		return returnMap;
	}

	private Map<String, Map> organizeViewMapAsXml(List events) {
		Calendar now=Calendar.getInstance();
		Map<String, Map> returnMap=new LinkedHashMap<String, Map>();
		for (Object object : events) {
			EventForView eventforview=(EventForView) object;
			Map<String,Object> temp2=new HashMap<String,Object>();
			int cliptype=eventforview.getCliptype();
			temp2.put("eventid", eventforview.getEventid());
			int checkeventid=eventforview.getReliedclip()>0?eventforview.getReliedclip():eventforview.getEventid();
			boolean isinvited=this.eventService.checkIsInvitedEvent(checkeventid);
			temp2.put("cliptype", cliptype);
			temp2.put("isinvited", isinvited);
			temp2.put("isClocked", ""+eventforview.isClocked());
			temp2.put("eventtype", eventforview.getEventtype());
			temp2.put("starttime", eventforview.getStarttime().toString());
			temp2.put("endtime", eventforview.getEndtime().toString());
			temp2.put("iswholeday", eventforview.getIswholeday().toString());
			temp2.put("content", eventforview.getContent());
			temp2.put("clocktime", eventforview.getClocktime()==null?"":eventforview.getClocktime());
			temp2.put("address", eventforview.getAddress());
			temp2.put("title", eventforview.getTitle());
//			temp2.put("deadlinetime", eventforview.getDeadlinetime().toString());
//			temp2.put("isexpire", eventforview.getDeadlinetime()<=now.getTimeInMillis());
			temp2.put("reliedclip", eventforview.getReliedclip().toString());
			temp2.put("isvip", eventforview.getIsvip().toString());
			temp2.put("isread", eventforview.getIsRead().toString());
			temp2.put("processstatus", eventforview.getProcessstatus().toString());
			temp2.put("clipstatus", eventforview.getClipstatus().toString());
			temp2.put("publishname", eventforview.getPublishname());
			temp2.put("forwardname", eventforview.getForwardname());
			temp2.put("puid", eventforview.getPuid());
			temp2.put("fuid", eventforview.getFuid());
			temp2.put("cliptype", eventforview.getCliptype());
			temp2.put("createtime", eventforview.getCreatetime());
			//temp2.put("date", firstDateOfWeek.getTimeInMillis());
			returnMap.put("EventForView id="+eventforview.getEventid(), temp2);
		}
		return returnMap;
	}
	
	private Map<String, Map> organizeCacheViewMapAsXml(List events) {
		Map<String, Map> returnMap=new LinkedHashMap<String, Map>();
		for (Object object : events) {
			EventForView eventforview=(EventForView) object;
			Map<String,Object> temp2=new HashMap<String,Object>();
			temp2.put("eventid", eventforview.getEventid());
			temp2.put("isClocked", ""+eventforview.isClocked());
			temp2.put("eventtype", eventforview.getEventtype());
			temp2.put("content", eventforview.getContent());
			temp2.put("clocktime", eventforview.getClocktime()==null?"":eventforview.getClocktime());
			temp2.put("address", eventforview.getAddress());
			temp2.put("title", eventforview.getTitle());
			temp2.put("isvip", eventforview.getIsvip().toString());
			temp2.put("clipstatus", eventforview.getClipstatus().toString());
			temp2.put("updatetime", eventforview.getUpdatetime());
			temp2.put("createtime", eventforview.getCreatetime());
			temp2.put("publishname", eventforview.getPublishname());
			//temp2.put("date", firstDateOfWeek.getTimeInMillis());
			returnMap.put("EventForView id="+eventforview.getEventid(), temp2);
		}
		return returnMap;
	}
	private Map<String, Map> organizeRemindViewMapAsXml(List events) {
		Map<String, Map> returnMap=new LinkedHashMap<String, Map>();
		Calendar now=Calendar.getInstance();
		for (Object object : events) {
			EventForView eventforview=(EventForView) object;
			Map<String,Object> temp2=new HashMap<String,Object>();
			temp2.put("eventid", eventforview.getEventid());
			int checkeventid=eventforview.getReliedclip()>0?eventforview.getReliedclip():eventforview.getEventid();
			boolean isinvited=this.eventService.checkIsInvitedEvent(checkeventid);
			temp2.put("cliptype", eventforview.getCliptype());
			temp2.put("isinvited", isinvited);
			temp2.put("isClocked", ""+eventforview.isClocked());
			temp2.put("eventtype", eventforview.getEventtype());
			temp2.put("starttime", eventforview.getStarttime().toString());
			temp2.put("endtime", eventforview.getEndtime().toString());
			temp2.put("iswholeday", eventforview.getIswholeday().toString());
			temp2.put("content", eventforview.getContent());
			temp2.put("clocktime", eventforview.getClocktime()==null?"":eventforview.getClocktime());
			temp2.put("address", eventforview.getAddress());
			temp2.put("title", eventforview.getTitle());
			temp2.put("deadlinetime", eventforview.getDeadlinetime().toString());
			temp2.put("isexpire", eventforview.getDeadlinetime()<=now.getTimeInMillis());
			temp2.put("isvip", eventforview.getIsvip().toString());
			temp2.put("processstatus", eventforview.getProcessstatus().toString());
			temp2.put("clipstatus", eventforview.getClipstatus().toString());
			temp2.put("alertstr", eventforview.getClocktime());
			temp2.put("reliedclip", eventforview.getReliedclip().toString());
			temp2.put("alertclock", eventforview.getClocktime());
			temp2.put("publishname", eventforview.getPublishname());
			temp2.put("forwardname", eventforview.getForwardname());
			temp2.put("puid", eventforview.getPuid());
			temp2.put("fuid", eventforview.getFuid());
			//temp2.put("date", firstDateOfWeek.getTimeInMillis());
			returnMap.put("EventForView id="+eventforview.getEventid(), temp2);
		}
		return returnMap;
	}
	
	private String formatCreatetime(Long createtime) {
		if(createtime==0l)
			return "";
		Calendar calendar=Calendar.getInstance();
		calendar.setTimeInMillis(createtime);
		Calendar now=Calendar.getInstance();
		if(calendar.get(Calendar.YEAR)==now.get(Calendar.YEAR)&&calendar.get(Calendar.MONTH)==now.get(Calendar.MONTH)&&calendar.get(Calendar.DAY_OF_MONTH)==now.get(Calendar.DAY_OF_MONTH)){
			SimpleDateFormat dateFormat=new SimpleDateFormat("今天	HH:mm");
			return dateFormat.format(calendar.getTime());
		}else{
			if(calendar.get(Calendar.YEAR)==now.get(Calendar.YEAR)){
				SimpleDateFormat dateFormat=new SimpleDateFormat("MM月dd日");
				return dateFormat.format(calendar.getTime());
			}else{
				SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
				return dateFormat.format(calendar.getTime());
			}
		}
	}

	private Map<String, Map> organizeTodayAndTommorrowViewMapAsXml(List events1, List events2) {
		Map<String, Map> returnMap=new LinkedHashMap<String, Map>();
		for (Object object : events1) {
			EventForView eventforview=(EventForView) object;
			Map<String,Object> temp2=new HashMap<String,Object>();
			temp2.put("eventid", eventforview.getEventid());
			temp2.put("isClocked", ""+eventforview.isClocked());
			temp2.put("eventtype", eventforview.getEventtype());
			temp2.put("starttime", eventforview.getStarttime().toString());
			temp2.put("endtime", eventforview.getEndtime().toString());
			temp2.put("iswholeday", eventforview.getIswholeday().toString());
			temp2.put("content", eventforview.getContent());
			temp2.put("clocktime", eventforview.getClocktime()==null?"":eventforview.getClocktime());
			temp2.put("address", eventforview.getAddress());
			temp2.put("title", eventforview.getTitle());
			temp2.put("deadlinetime", eventforview.getDeadlinetime().toString());
			temp2.put("reliedclip", eventforview.getReliedclip().toString());
			temp2.put("isvip", eventforview.getIsvip().toString());
			temp2.put("processstatus", eventforview.getProcessstatus().toString());
			temp2.put("publishname", eventforview.getPublishname());
			//temp2.put("date", firstDateOfWeek.getTimeInMillis());
			returnMap.put("EventForView dayattr="+1+" id="+eventforview.getEventid(), temp2);
		}
		for (Object object : events2) {
			EventForView eventforview=(EventForView) object;
			Map<String,Object> temp2=new HashMap<String,Object>();
			temp2.put("eventid", eventforview.getEventid());
			temp2.put("isClocked", ""+eventforview.isClocked());
			temp2.put("eventtype", eventforview.getEventtype());
			temp2.put("starttime", eventforview.getStarttime().toString());
			temp2.put("endtime", eventforview.getEndtime().toString());
			temp2.put("content", eventforview.getContent());
			temp2.put("clocktime", eventforview.getClocktime()==null?"":eventforview.getClocktime());
			temp2.put("address", eventforview.getAddress());
			temp2.put("title", eventforview.getTitle());
			temp2.put("deadlinetime", eventforview.getDeadlinetime().toString());
			temp2.put("reliedclip", eventforview.getReliedclip().toString());
			temp2.put("isvip", eventforview.getIsvip().toString());
			temp2.put("processstatus", eventforview.getProcessstatus().toString());
			temp2.put("publishname", eventforview.getPublishname());
			//temp2.put("date", firstDateOfWeek.getTimeInMillis());
			returnMap.put("EventForView dayattr="+2+" id="+eventforview.getEventid(), temp2);
		}
		return returnMap;
	}

	public void loadCCShareInfo(){
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		ClipEvent clipEvent=this.eventService.getDetailClipEvent(eventid);
		List list=this.eventService.getDetailStatAboutClipEvent(clipEvent,currentUser); //得到分享档期的统计
		Long interestcount=(Long) list.get(0);
		Long forwardcount=(Long) list.get(1);
		Long favorcount=(Long) list.get(2);
		JSONObject returnObj=new JSONObject();
		returnObj.put("interestcount", interestcount);
		returnObj.put("forwardcount", forwardcount);
		returnObj.put("favorcount", favorcount);
		int dealeventid=0;
		if(clipEvent.getSeid()>0){
			dealeventid=clipEvent.getSeid();
		}else{
			dealeventid=clipEvent.getId();
		}
		if(interestcount>0l){
			List<Integer> uidlist=this.eventService.loadPagingInterestUsers(dealeventid,currentUser,0,this.DETAILUSERSLENGTH);
			BigDecimal br = new BigDecimal(interestcount).divide(new BigDecimal(this.DETAILUSERSLENGTH),BigDecimal.ROUND_UP);
			Integer pageCount = (br.doubleValue()<1&&br.doubleValue()>=0 ? 1 : br.intValue());
			JSONArray array=new JSONArray();
			for (Integer uid : uidlist) {
				JSONObject temp=new JSONObject();
				List logos=this.userService.getUserLogoByUid(uid);
				String biglogo=(String) logos.get(0);
				String smalllogo=(String) logos.get(1);
				String name=this.contactService.getUserName(currentUser.getId(), uid);
				temp.put("uid",uid);
				temp.put("name",name);
				temp.put("smalllogo",smalllogo);
				temp.put("biglogo",biglogo);
				array.add(temp);
			}
			returnObj.put("interestpagecount", pageCount);
			returnObj.put("interestusers", array);
		}
		if(forwardcount>0l){
			List<Integer> uidlist=this.eventService.loadPagingForwardUsers(dealeventid,0,this.DETAILUSERSLENGTH);
			BigDecimal br = new BigDecimal(forwardcount).divide(new BigDecimal(this.DETAILUSERSLENGTH),BigDecimal.ROUND_UP);
			Integer pageCount = (br.doubleValue()<1&&br.doubleValue()>=0 ? 1 : br.intValue());
			JSONArray array=new JSONArray();
			for (Integer uid : uidlist) {
				JSONObject temp=new JSONObject();
				List logos=this.userService.getUserLogoByUid(uid);
				String biglogo=(String) logos.get(0);
				String smalllogo=(String) logos.get(1);
				String name=this.contactService.getUserName(currentUser.getId(), uid);
				temp.put("uid",uid);
				temp.put("name",name);
				temp.put("smalllogo",smalllogo);
				temp.put("biglogo",biglogo);
				array.add(temp);
			}
			returnObj.put("forwardpagecount", pageCount);
			returnObj.put("forwardusers", array);
		}
		if(favorcount>0l){
			List<Integer> uidlist=this.eventService.loadPagingFavorUsers(dealeventid,0,this.DETAILUSERSLENGTH);
			BigDecimal br = new BigDecimal(favorcount).divide(new BigDecimal(this.DETAILUSERSLENGTH),BigDecimal.ROUND_UP);
			Integer pageCount = (br.doubleValue()<1&&br.doubleValue()>=0 ? 1 : br.intValue());
			JSONArray array=new JSONArray();
			for (Integer uid : uidlist) {
				JSONObject temp=new JSONObject();
				List logos=this.userService.getUserLogoByUid(uid);
				String biglogo=(String) logos.get(0);
				String smalllogo=(String) logos.get(1);
				String name=this.contactService.getUserName(currentUser.getId(), uid);
				temp.put("uid",uid);
				temp.put("name",name);
				temp.put("smalllogo",smalllogo);
				temp.put("biglogo",biglogo);
				array.add(temp);
			}
			returnObj.put("favorpagecount", pageCount);
			returnObj.put("favorusers", array);
		}
		Struts2Utils.renderJson(returnObj.toJSONString());
	}
	
	public void pagingShareInfo(){
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		List<Integer> uidlist=this.eventService.getPagingShareInfo(eventid,currentUser,pageclass,currentpage,this.DETAILUSERSLENGTH);
		JSONObject returnObj=new JSONObject();
		if(uidlist!=null&&uidlist.size()>0){
			returnObj.put("result", 1);
			JSONArray array=new JSONArray();
			for (Integer uid : uidlist) {
				JSONObject temp=new JSONObject();
				List logos=this.userService.getUserLogoByUid(uid);
				String biglogo=(String) logos.get(0);
				String smalllogo=(String) logos.get(1);
				String name=this.contactService.getUserName(currentUser.getId(), uid);
				temp.put("uid",uid);
				temp.put("name",name);
				temp.put("smalllogo",smalllogo);
				temp.put("biglogo",biglogo);
				array.add(temp);
			}
			returnObj.put("users", array);
		}else{
			returnObj.put("result", 0);
		}
		Struts2Utils.renderJson(returnObj.toJSONString());
	}
	
	public void loadPPShareInfo(){
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		ClipEvent clipEvent=this.eventService.getDetailClipEvent(eventid);
		if(clipEvent.getEventtype()==2&&(clipEvent.getSeid()==0||clipEvent.getSeid().equals(clipEvent.getReliedclip()))){
			//直接看商家 / 看自己【感】商家的档期
			JSONObject returnObj=new JSONObject();
			List<Integer> uidlist=this.eventService.findHaveInterestFriends(currentUser.getId(), eventid);
			if(uidlist!=null&&uidlist.size()>0){
				returnObj.put("size", uidlist.size());
				JSONArray array=new JSONArray();
				for (Integer uid : uidlist) {
					JSONObject temp=new JSONObject();
					List logos=this.userService.getUserLogoByUid(uid);
					String biglogo=(String) logos.get(0);
					String smalllogo=(String) logos.get(1);
					String name=this.contactService.getUserName(currentUser.getId(), uid);
					temp.put("uid",uid);
					temp.put("name",name);
					temp.put("smalllogo",smalllogo);
					temp.put("biglogo",biglogo);
					temp.put("eventisinterested", 1);
					array.add(temp);
				}
				returnObj.put("users", array);
			}else{
				returnObj.put("size", 0);
			}
			returnObj.put("clipiscc", true);
			Struts2Utils.renderJson(returnObj.toJSONString());
		}else{
			JSONObject returnObj=new JSONObject();
			List share2users=null;
			int dealeventid=0;
			if(clipEvent.getReliedclip()>0)
				dealeventid=clipEvent.getReliedclip();
			else
				dealeventid=clipEvent.getId();
			share2users=this.eventService.getShare2users(dealeventid);
			List interestusers=this.eventService.findHaveInterestFriends(currentUser.getId(),dealeventid);
			JSONArray array=new JSONArray();
			List temp1=new ArrayList();//放已感兴趣的
			List temp2=new ArrayList();//放未感兴趣的
			for (Object object : share2users) {
				Sharemapping sharemapping=(Sharemapping) object;
				int uid=sharemapping.getShare2uid();
				if(uid==currentUser.getId())
					continue;
				if(interestusers.contains(uid)){
					temp1.add(uid);
				}else{
					temp2.add(uid);
				}
			}
			for (Object object : temp1) {
				Integer uid=(Integer) object;
				JSONObject temp=new JSONObject();
				List logos=this.userService.getUserLogoByUid(uid);
				String biglogo=(String) logos.get(0);
				String smalllogo=(String) logos.get(1);
				String name=this.contactService.getUserName(currentUser.getId(), uid);
				temp.put("uid",uid);
				temp.put("name",name);
				temp.put("smalllogo",smalllogo);
				temp.put("biglogo",biglogo);
				temp.put("eventisinterested", 1);
				array.add(temp);
			}
			for (Object object : temp2) {
				Integer uid=(Integer) object;
				JSONObject temp=new JSONObject();
				List logos=this.userService.getUserLogoByUid(uid);
				String biglogo=(String) logos.get(0);
				String smalllogo=(String) logos.get(1);
				String name=this.contactService.getUserName(currentUser.getId(), uid);
				temp.put("uid",uid);
				temp.put("name",name);
				temp.put("smalllogo",smalllogo);
				temp.put("biglogo",biglogo);
				temp.put("eventisinterested", 0);
				array.add(temp);
			}
			returnObj.put("users", array);
			returnObj.put("size", share2users.size());
			returnObj.put("clipiscc", false);
			Struts2Utils.renderJson(returnObj.toJSONString());
		}
	}
	
	public void loadRecycleclip(){
		HttpServletRequest request=Struts2Utils.getRequest();
		User currentUser=(User) Struts2Utils.getSession().getAttribute(SessionAttributeKey.USER);
		Calendar now=Calendar.getInstance();
		
		JSONObject returnObj=new JSONObject();
		JSONObject sub1Return=new JSONObject();
		ClipEvent clipEvent=null;
		if(eventid!=null&&eventid>0){
			clipEvent=this.eventService.getDetailClipEvent(eventid);
		}
		if(clipEvent==null){
			returnObj.put("result", 0);
			returnObj.put("errormsg", "档期不存在或无权查看");
			Struts2Utils.renderJson(returnObj.toJSONString());
			return;
		}else{
			boolean result=this.eventService.checkIsshare2user(eventid,currentUser.getId());
			if(!result){
				returnObj.put("result", 0);
				returnObj.put("errormsg", "档期不存在或无权查看");
				Struts2Utils.renderJson(returnObj.toJSONString());
			}else{
				sub1Return.put("eventid", clipEvent.getId());
				sub1Return.put("title", clipEvent.getTitle());
				
				sub1Return.put("starttime",clipEvent.getBegintime());
				sub1Return.put("endtime", clipEvent.getEndtime());
				sub1Return.put("iswholeday", clipEvent.getIswholeday());
				String content=clipEvent.getContent();
				content=content==null||content.length()==0?"":content;
				sub1Return.put("content", URLReplace.replace(content));
				sub1Return.put("clocktime", clipEvent.getAlerttime());
				sub1Return.put("reliedclip",0);
				sub1Return.put("shareid", clipEvent.getShareid());
					List fuidlogourllist=this.userService.getUserLogoByUid(clipEvent.getUid());
					String fuidcontactname=this.contactService.getUserName(currentUser.getId(),clipEvent.getUid());
					User clipUser=this.userService.getUserById(clipEvent.getUid());
					sub1Return.put("uname", fuidcontactname);
					sub1Return.put("unameid",clipEvent.getUid());
					sub1Return.put("unamelogo", fuidlogourllist.get(0));
					sub1Return.put("unameslogo", fuidlogourllist.get(1));
					if(clipUser.getUsertype()==2){
						sub1Return.put("clipiscc", 1);
					}else{
						sub1Return.put("clipiscc", 0);
					}
				sub1Return.put("seid", clipEvent.getSeid());
				if(clipEvent.getSeid()>0&&clipEvent.getEventtype()==2){
					ClipEvent sourceEvent=this.eventService.getDetailClipEvent(clipEvent.getSeid());
					User sourceUser=this.userService.getUserById(sourceEvent.getUid());
					sub1Return.put("sourceeventid", sourceEvent.getId());
					sub1Return.put("sourceuid", sourceEvent.getUid());
					sub1Return.put("sourceuname", this.contactService.getUserName(currentUser.getId(), sourceUser.getId()));
					List sourceuserlist=this.userService.getUserLogoByUid(sourceUser.getId());
					sub1Return.put("sourceulogo", sourceuserlist.get(1));
				}
				ClipeventPic clipeventPic=this.eventService.getClipeventPic(clipEvent.getId());
				if(clipeventPic!=null){
					sub1Return.put("clipeventpic", clipeventPic.getPicurl());
				}else{
					sub1Return.put("clipeventpic", "");
				}
				sub1Return.put("cliptype", clipEvent.getCliptype());
				sub1Return.put("clipstatus",clipEvent.getStatus());
				sub1Return.put("eventtype",clipEvent.getEventtype());
				sub1Return.put("processstatus",clipEvent.getProcessstatus());
				returnObj.put("result", 1);
				returnObj.put("clipevent", sub1Return);
				returnObj.put("systemmsgid", this.messageService.findBymessageid(currentUser.getId(),clipEvent.getId(),SystemMessage.CANCELTYPE));
				Struts2Utils.renderJson(returnObj.toJSONString());
			}
			
		}
		
	}
	
	private Integer eventid;
	private String shareid;
	private Long definedLong;
	private String queryby;
	private Integer currentpage;
	private String content;
	private Integer year=0;
	private Integer month=0;
	private Integer date=0;
	private String orderby;
	private int isDesc;
	private String pageclass;
	public Integer getEventid() {
		return eventid;
	}

	public void setEventid(Integer eventid) {
		this.eventid = eventid;
	}

	public Long getDefinedLong() {
		return definedLong;
	}

	public void setDefinedLong(Long definedLong) {
		this.definedLong = definedLong;
	}

	public String getQueryby() {
		return queryby;
	}

	public void setQueryby(String queryby) {
		this.queryby = queryby;
	}

	public Integer getCurrentpage() {
		return currentpage;
	}

	public void setCurrentpage(Integer currentpage) {
		this.currentpage = currentpage;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Integer getYear() {
		return year;
	}

	public void setYear(Integer year) {
		this.year = year;
	}

	public Integer getMonth() {
		return month;
	}

	public void setMonth(Integer month) {
		this.month = month;
	}

	public Integer getDate() {
		return date;
	}

	public void setDate(Integer date) {
		this.date = date;
	}

	public String getOrderby() {
		return orderby;
	}

	public void setOrderby(String orderby) {
		this.orderby = orderby;
	}

	public int getIsDesc() {
		return isDesc;
	}

	public void setIsDesc(int isDesc) {
		this.isDesc = isDesc;
	}

	public String getShareid() {
		return shareid;
	}

	public void setShareid(String shareid) {
		this.shareid = shareid;
	}

	public String getPageclass() {
		return pageclass;
	}

	public void setPageclass(String pageclass) {
		this.pageclass = pageclass;
	}
	
	
}
